package cn.wangzg.mymusic;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import java.io.IOException;
import java.util.List;

public class MusicService extends Service {
    private MediaPlayer mediaPlayer;
    private int seekLength = 0;
    private int currentIndex = -1;
    private List<Music> musicList;

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        musicList = MusicUtils.getMusic(getApplicationContext());
    }

    private class MyBinder extends Binder implements MyBinderInterface{

        @Override
        public void Pause() {
            if (mediaPlayer.isPlaying()){
                mediaPlayer.pause();
                seekLength = mediaPlayer.getCurrentPosition();
            }
        }

        @Override
        public void Resume() {
            mediaPlayer.seekTo(seekLength);
            mediaPlayer.start();
        }

        @Override
        public void Play() {
            mediaPlayer.reset();
            Uri path = Uri.parse(musicList.get(currentIndex).getData());

            try {
                mediaPlayer.setDataSource(String.valueOf(path));
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.seekTo(seekLength);
            mediaPlayer.start();
            sendInfo(currentIndex);
        }

        @Override
        public void PlayNext() {
            currentIndex += 1;
            if (currentIndex >= musicList.size()){
                currentIndex = 0;
            }
            seekLength = 0;
            Play();
        }

        @Override
        public void PlayPrev() {
            currentIndex -= 1;
            if (currentIndex <= 0){
                currentIndex = musicList.size() - 1;
            }
            seekLength = 0;
            Play();
        }

        @Override
        public void Release() {
            mediaPlayer.reset();
            mediaPlayer.stop();
            mediaPlayer.release();
        }

        @Override
        public boolean isPlaying() {
            return mediaPlayer.isPlaying();
        }

        @Override
        public int getDuration() {
            return mediaPlayer.getDuration();
        }

        @Override
        public int getCurrentPosition() {
            return mediaPlayer.getCurrentPosition();
        }

        @Override
        public void seekTo(int length) {
            seekLength = length;
            mediaPlayer.seekTo(length);
        }

        @Override
        public int getCurrentIndex() {
            return currentIndex;
        }

        @Override
        public void setCurrentIndex(int currentIdx) {
            currentIndex = currentIdx;
        }

        @Override
        public void sendInfo(int currentIndex) {
            Intent intent = new Intent();
            intent.putExtra("title",musicList.get(currentIndex).getTitle());
            intent.putExtra("artist",musicList.get(currentIndex).getArtist());
            intent.putExtra("pic",musicList.get(currentIndex).getAlbumId());
            intent.setAction("music");
            sendBroadcast(intent);
        }
    }
}